package com.tcc.fastline.validators;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.tcc.fastline.Helpers.DateTime;

import android.util.Log;

public class DateValidator {

	private static Pattern pattern;
	private static Matcher matcher;

	private static final String DATE_PATTERN = "(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";
	
	public static boolean validar(final String dateFormatoServidor) {

		Date dataServidor = DateTime.convertDateTimeServidor(dateFormatoServidor);
		final String date = DateTime.formatDate(dataServidor);
		
		pattern = Pattern.compile(DATE_PATTERN);
		matcher = pattern.matcher(date);

		if (matcher.matches()) {

			matcher.reset();

			if (matcher.find()) {

				String day = matcher.group(1);
				Log.e("DIA", day);
				String month = matcher.group(2);
				Log.e("MES", month);
				int year = Integer.parseInt(matcher.group(3));
				Log.e("ANO", String.valueOf(year));

				if (day.equals("31")
						&& (month.equals("4") || month.equals("6")
								|| month.equals("9") || month.equals("11")
								|| month.equals("04") || month.equals("06") || month
									.equals("09"))) {
					Log.e("Data", "INValida");
					return false; // only 1,3,5,7,8,10,12 has 31 days
				} else if (month.equals("2") || month.equals("02")) {
					// leap year
					if (year % 4 == 0) {
						if (day.equals("30") || day.equals("31")) {
							Log.e("Data", "Invalida");
							return false;
						} else {
							Log.e("Data", "Valida");
							return true;
						}
					} else {
						if (day.equals("29") || day.equals("30")
								|| day.equals("31")) {
							Log.e("Data", "InValida");
							return false;
						} else {
							Log.e("Data", "Valida");
							return true;
						}
					}
				} else {
					Log.e("Data", "Valida");
					return true;
				}
			} else {
				Log.e("Data", "INValida");
				return false;
			}
		} else {
			Log.e("Data", "INValida");
			return false;
		}
	}

}
